/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	//global vars
	var form = $("#customForm");
	var fname = $("#fname");
	var fnameInfo = $("#fnameInfo");
	var mname = $("#mname");
	var mnameInfo = $("#mnameInfo");
	var lname = $("#lname");
	var lnameInfo = $("#lnameInfo");
	var email = $("#email");
	var emailInfo = $("#emailInfo");
	var bday = $("#datepicker");
	var bdayInfo = $("#bdayInfo");
	var sex = $('input[name=sex]');
	var sexInfo = $("#sexInfo");
	
	//createSection page
	var addSection = $("#addSection");
	var secName = $("#secName");
	var secAd = $("#secAd");
	var rName = $("#rName");
	var secNameInfo = $("#secNameInfo");
	var secAdInfo = $("#secAdInfo");
	var rNameInfo = $("#rNameInfo");
	
	//createReplace page
	var addReplace = $("#addReplace");
	var repName = $("#repName");
	var repNameInfo = $("#repNameInfo");
	//On blur
	bday.change(validateDOB);
	fname.blur(validateFname);
	mname.blur(validateMname);
	lname.blur(validateLname);
	sex.blur(validateSex);
	email.blur(validateEmail);
	
	//createSection page
	secName.blur(validateSecName);
	secAd.blur(validateSecAd);
	rName.blur(validateRoomName);
	//create Replace page
	repName.blur(validateRep);
	//On key press
	fname.keyup(validateFname);
	mname.keyup(validateMname);
	lname.keyup(validateLname);
	email.keyup(validateEmail);
	sex.keyup(validateSex);
	//createSection page
	secName.keyup(validateSecName);
	secAd.keyup(validateSecAd);
	rName.keyup(validateRoomName);
	//create Replace page
	repName.keyup(validateRep);
	//On Submitting
	form.submit(function(){
		if(validateFname() & validateLname() &  validateSex() & validateDOB() & validateMname() & validateEmail())
			return true
			
		else
			return false;
	});
	
	addSection.submit(function() {
		if(validateSecName() & validateSecAd() & validateRoomName())
			return true
		else
			return false;
	});
	addReplace.submit(function() {
		
		if(validateRep())
			return true
		else
			return false;
	});
	//validation functions
	function validateEmail(){
		//testing regular expression
		var a = $("#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		
		//if it's valid email
		if(filter.test(a)){
			email.removeClass("error");
			emailInfo.text("e.i. username@domain.com");
			emailInfo.removeClass("error");
			return true;
		}
		//if it's NOT valid
		else{
			email.addClass("error");
			emailInfo.text("Please enter valid e-mail! System won't accept that");
			emailInfo.addClass("error");
			return false;
		}
	}
	function validateFname(){
		//if it's NOT valid	
		var a = $("#fname").val();
		var filter1 = /^[a-zA-Z][a-zA-Z]+$/;
		var filter2 = /^([a-zA-Z][a-zA-Z]+[\s])([a-zA-Z][a-zA-Z]+[\s])*[a-zA-Z][a-zA-Z]+$/;
		if(filter1.test(a) || filter2.test(a)){
			fname.removeClass("error");
			fname.addClass("okay");
			fnameInfo.text("Firstname Ok!");
			fnameInfo.addClass("okay");
			fnameInfo.removeClass("error");
			return true;			
		}else if(fname.val().length < 3){
			fname.addClass("error");
			fnameInfo.text("Please enter more than 2 letters!");
			fnameInfo.addClass("error");
			return false;
		}
		else {
			fname.addClass("error");
			fnameInfo.text("Invalid entry!");
			fnameInfo.addClass("error");
			return false;
			
		}
	}
	

	//middlename
	function validateMname(){
		//if it's NOT valid
		if(mname.val().length < 2){
			mname.addClass("error");
			mnameInfo.text("We want Middlename with more than 1 letters!");
			mnameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			mname.removeClass("error");
			mnameInfo.text("What's your Middlename?");
			mnameInfo.removeClass("error");
			return true;
		}
	}
	
	//lastname
	function validateLname(){
		//if it's NOT valid
		if(lname.val().length < 2){
			lname.addClass("error");
			lnameInfo.text("We want Lastname with more than 3 letters!");
			lnameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			lname.removeClass("error");
			lnameInfo.text("What's your Lastname?");
			lnameInfo.removeClass("error");
			return true;
		}
	}
	
	
	//gender
	function validateSex(){
		//if it's NOT valid	
		var sex = $('input[name=sex]:checked').val() 
		var sex = sex+"";
		if(sex.length==9){
			sexInfo.text("Please make a selection!");
			sexInfo.addClass("error");
			return false;
		}else{
			sexInfo.text("Gender Ok!");
			sexInfo.removeClass("error");
			return true;
		}	
	}
	
	//bday
	
	function validateDOB(){
		
		var a = $("#datepicker").val();
		var filter = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/;
		if(filter.test(a)){
			bday.removeClass("error");
			bdayInfo.text("");
			bdayInfo.removeClass("error");
			return true;
		}
		//if it's NOT valid
		else{
			bday.addClass("error");
			bdayInfo.text("Type a valid date!");
			bdayInfo.addClass("error");
			return false;
		}
	}
	
	
	//Replace Name
	
		function validateRep(){
		//if it's NOT valid	
		var a = $("#repName").val();
		var filter1 = '[-+]?\b\d+\b';
	
		if(filter1.test(a)){
			repName.removeClass("error");
			repName.addClass("okay");
			repNameInfo.text("Replace Name Ok!");
			repNameInfo.addClass("okay");
			repNameInfo.removeClass("error");
			return true;			
		}else if(repName.val().length < 3){
			repName.addClass("error");
			repNameInfo.text("We want Name with more than 2 letters!");
			repNameInfo.addClass("error");
			return false;
		}
		else {
			repName.addClass("error");
			repNameInfo.text("This name cannot be accept!");
			repNameInfo.addClass("error");
			return false;
			
		}
	}
	
		//Orig Name
	
		function validateOrig(){
		//if it's NOT valid	
		var a = $("#origName").val();
		var filter1 = '[-+]?\b\d+\b';
	
		if(filter1.test(a)){
			origName.removeClass("error");
			origName.addClass("okay");
			origNameInfo.text("Replace Name Ok!");
			origNameInfo.addClass("okay");
			origNameInfo.removeClass("error");
			return true;			
		}else if(origName.val().length < 3){
			origName.addClass("error");
			origNameInfo.text("We want Name with more than 2 letters!");
			origNameInfo.addClass("error");
			return false;
		}
		else {
			origName.addClass("error");
			origNameInfo.text("This name cannot be accept!");
			origNameInfo.addClass("error");
			return false;
			
		}
	}
	//secAd
	function validateSecAd(){
		//if it's NOT valid	
		var a = $("#secAd").val();
		var filter1 = /^[a-zA-Z][a-zA-Z]+$/;
		var filter2 = /^([a-zA-Z][a-zA-Z]+[\s])([a-zA-Z][a-zA-Z]+[\s])*[a-zA-Z][a-zA-Z]+$/;
		if(filter1.test(a) || filter2.test(a)){
			secAd.removeClass("error");
			secAd.addClass("okay");
			secAdInfo.text("Section Adviser Ok!");
			secAdInfo.addClass("okay");
			secAdInfo.removeClass("error");
			return true;			
		}else if(secAd.val().length < 3){
			secAd.addClass("error");
			secAdInfo.text("We want Name with more than 2 letters!");
			secAdInfo.addClass("error");
			return false;
		}
		else {
			secAd.addClass("error");
			secAdInfo.text("This name cannot be accept!");
			secAdInfo.addClass("error");
			return false;
			
		}
	}
	
	//section Name
	
	function validateSecName(){
		//if it's NOT valid	
		var a = $("#secName").val();
		var filter1 = /^[a-zA-Z][a-zA-Z]+$/;
		var filter2 = /^([a-zA-Z][a-zA-Z]+[\s])([a-zA-Z][a-zA-Z]+[\s])*[a-zA-Z][a-zA-Z]+$/;
		if(filter1.test(a) || filter2.test(a)){
			secName.removeClass("error");
			secName.addClass("okay");
			secNameInfo.text("Section name Ok!");
			secNameInfo.addClass("okay");
			secNameInfo.removeClass("error");
			return true;			
		}else if(secName.val().length < 3){
			secName.addClass("error");
			secNameInfo.text("We want Name with more than 2 letters!");
			secNameInfo.addClass("error");
			return false;
		}
		else {
			secName.addClass("error");
			secNameInfo.text("This name cannot be accept!");
			secNameInfo.addClass("error");
			return false;
			
		}
	}
	
	//room name
	
	function validateRoomName(){
		//if it's NOT valid	
		var a = $("#rName").val();
		var filter1 = /^[a-zA-Z][a-zA-Z]+$/;
		var filter2 = /^([a-zA-Z][a-zA-Z]+[\s])([a-zA-Z][a-zA-Z]+[\s])*[a-zA-Z][a-zA-Z]+$/;
		if(filter1.test(a) || filter2.test(a)){
			rName.removeClass("error");
			rName.addClass("okay");
			rNameInfo.text("Room Name Ok!");
			rNameInfo.addClass("okay");
			rNameInfo.removeClass("error");
			return true;			
		}else if(rName.val().length < 3){
			rName.addClass("error");
			rNameInfo.text("We want Name with more than 2 letters!");
			rNameInfo.addClass("error");
			return false;
		}
		else {
			rName.addClass("error");
			rNameInfo.text("This name cannot be accept!");
			rNameInfo.addClass("error");
			return false;
			
		}
	}
	
	
});